import 'dart:math';

import 'package:flutter/material.dart';
import 'package:kasax_demos2/entry/data_posts_test.dart';
import 'package:kasax_demos2/entry/entrys.dart';
import 'package:kasax/utils/img_utils.dart';
import 'package:fluttertoast/fluttertoast.dart';

class PostDetailPage extends StatefulWidget {
  final Posts posts;

  const PostDetailPage(this.posts, {Key? key}) : super(key: key);

  @override
  _PostDetailPageState createState() => _PostDetailPageState();
}

class _PostDetailPageState extends State<PostDetailPage> {
  List<Posts> _list = [];

  @override
  void initState() {
    // 随机数据
    for (int i = 0; i < PostsDatas.TEST_POSTS.length; i++) {
      _list.add(PostsDatas
          .TEST_POSTS[Random().nextInt(PostsDatas.TEST_POSTS.length)]);
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: CustomScrollView(
          slivers: [
            SliverAppBar(
              pinned: true,
              expandedHeight: 250,
              flexibleSpace: FlexibleSpaceBar(
                background: Image.network(
                  widget.posts.imageUrl,
                  fit: BoxFit.cover,
                ),
                centerTitle: true,
                title: Text(widget.posts.title),
              ),
            ),
            SliverPadding(
              padding: EdgeInsets.all(8),
              sliver: SliverList(
                delegate: SliverChildBuilderDelegate((context, index) {
                  var info = _list[index];
                  return buildContainerContent2(info, index);
                }, childCount: _list.length),
              ),
            )
          ],
        ),
      ),
    );
  }

  /// 使得Inkwell处理点击事件
  Container buildContainerContent1(Posts info, int index) {
    return Container(
      margin: EdgeInsets.only(top: 15),
      child: Stack(
        children: [
          Container(
            child: Column(
              children: [
                ImageUtils.buildRoundImgByClipRRect(
                    16 / 9, 8.0, "${info.imageUrl}"),
                SizedBox(
                  height: 5,
                ),
                Row(
                  children: [
                    Text("${widget.posts.title}"),
                    Expanded(child: Container()),
                    Text("${widget.posts.author}"),
                  ],
                ),
                Divider(
                  indent: 45,
                  endIndent: 45,
                ),
                SizedBox(
                  height: 5,
                ),
                Text(
                  widget.posts.description,
                  maxLines: 3,
                  overflow: TextOverflow.ellipsis,
                ),
              ],
            ),
          ),
          Positioned(
            child: CircleAvatar(
              child: Text("$index"),
            ),
            top: 10,
            right: 10,
          ),
          Positioned.fill(
              child: InkWell(
            splashColor: Colors.white.withOpacity(0.3),
            highlightColor: Colors.lightBlue.withOpacity(0.1),
            onTap: () {
              print("选择了：${info.title}");
            },
          ))
        ],
      ),
    );
  }

  /// 使用处理点击事件
  Widget buildContainerContent2(Posts info, int index) {
    return Container(
      child: GestureDetector(
        onTap: () {
          print("选择了：${info.title}");
          Fluttertoast.showToast(msg: "选择了：${info.title}");
        },
        child: Container(
          margin: EdgeInsets.only(top: 15),
          child: Stack(
            children: [
              Container(
                child: Column(
                  children: [
                    ImageUtils.buildRoundImgByClipRRect(
                        16 / 9, 8.0, "${info.imageUrl}"),
                    SizedBox(
                      height: 5,
                    ),
                    Row(
                      children: [
                        Text("${widget.posts.title}"),
                        Expanded(child: Container()),
                        Text("${widget.posts.author}"),
                      ],
                    ),
                    Divider(
                      indent: 45,
                      endIndent: 45,
                    ),
                    SizedBox(
                      height: 5,
                    ),
                    Text(
                      widget.posts.description,
                      maxLines: 3,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ],
                ),
              ),
              Positioned(
                child: CircleAvatar(
                  child: Text("$index"),
                ),
                top: 10,
                right: 10,
              ),
              Positioned.fill(
                  child: InkWell(
                splashColor: Colors.white.withOpacity(0.3),
                highlightColor: Colors.lightBlue.withOpacity(0.1),
              ))
            ],
          ),
        ),
      ),
    );
  }
}
